package com.safety51.bi.mapper;

import com.safety51.bi.entity.ChartConfig;
import com.safety51.bi.entity.DimensionConfig;
import com.safety51.bi.entity.FilterConfig;
import com.safety51.bi.entity.MetricConfig;
import com.safety51.bi.util.JsonUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class ChartConfigMapper {

    private final JdbcTemplate jdbcTemplate;

    public ChartConfigMapper(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public ChartConfig findByChartId(String chartId) {
        String sql = "SELECT * FROM bi_chart_config WHERE chart_id = ? AND status = 1";
        try {
            return jdbcTemplate.queryForObject(sql, new ChartConfigRowMapper(), chartId);
        } catch (Exception e) {
            return null;
        }
    }

    public List<ChartConfig> findAllActive() {
        String sql = "SELECT * FROM bi_chart_config WHERE status = 1";
        return jdbcTemplate.query(sql, new ChartConfigRowMapper());
    }

    private static class ChartConfigRowMapper implements RowMapper<ChartConfig> {
        @Override
        public ChartConfig mapRow(ResultSet rs, int rowNum) throws SQLException {
            ChartConfig config = new ChartConfig();
            config.setId(rs.getLong("id"));
            config.setChartId(rs.getString("chart_id"));
            config.setChartName(rs.getString("chart_name"));
            config.setChartType(rs.getString("chart_type"));
            config.setChartSubtype(rs.getString("chart_subtype"));
            config.setDisplayType(rs.getString("display_type"));
            config.setDataSource(rs.getString("data_source"));
            config.setNumberFormat(rs.getString("number_format"));
            config.setCreatedTime(rs.getTimestamp("created_time"));
            config.setUpdatedTime(rs.getTimestamp("updated_time"));

            // 解析JSON字段
            String dimensionsJson = rs.getString("dimensions");
            String metricsJson = rs.getString("metrics");
            String filtersJson = rs.getString("filters");
            String compareConfigJson = rs.getString("compare_config");
            String exportConfigJson = rs.getString("export_config");
            String optionsTemplateJson = rs.getString("options_template");

            // 使用JSON工具类解析
            if (dimensionsJson != null) {
                List<DimensionConfig> dimensions = JsonUtil.parseJsonList(dimensionsJson, DimensionConfig.class);
                config.setDimensions(dimensions);
            }

            if (metricsJson != null) {
                List<MetricConfig> metrics = JsonUtil.parseJsonList(metricsJson, MetricConfig.class);
                config.setMetrics(metrics);
            }

            if (filtersJson != null) {
                List<FilterConfig> filters = JsonUtil.parseJsonList(filtersJson, FilterConfig.class);
                config.setFilters(filters);
            }

            if (compareConfigJson != null) {
                Map<String, Object> compareConfig = JsonUtil.parseJsonMap(compareConfigJson);
                // 这里简化处理，实际应该创建CompareConfig对象
                // config.setCompareConfig(JsonUtil.parseJson(compareConfigJson, CompareConfig.class));
            }

            if (exportConfigJson != null) {
                Map<String, Object> exportConfig = JsonUtil.parseJsonMap(exportConfigJson);
                // config.setExportConfig(JsonUtil.parseJson(exportConfigJson, ExportConfig.class));
            }

            if (optionsTemplateJson != null) {
                Map<String, Object> optionsTemplate = JsonUtil.parseJsonMap(optionsTemplateJson);
                config.setOptionsTemplate(optionsTemplate);
            }

            return config;
        }
    }
}